package com.hmis.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmis.domain.Patient;
import com.hmis.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@Controller
public class PatientDAO implements InitializingBean {

    public static PatientService patientService;
    public static PatientDAO patientDAO;

    //    public List<Patient> listPatients() {
//        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper();
//        queryWrapper.in(Patient::getStatus,0);
//        return patientService.list(queryWrapper);
//    }
    private List<Patient> patients;
    @Autowired
    private PatientService patient;

    public List<Patient> list() {
        return patientService.list();
    }

    public void appendPatient(Patient patient) {
        patientService.save(patient);
    }

    public void deletePatient(String patientName, Long wardNo, Long theBedNo) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Patient::getPatientName, patientName);
        queryWrapper.eq(Patient::getWardNo, wardNo);
        queryWrapper.eq(Patient::getTheBedNo, theBedNo);
        Patient patient = patientService.getOne(queryWrapper);
        patient.setStatus(1);
        patient.setIsDelete(1);
        patientService.updateById(patient);
    }

    public Patient findById(Long patientId) {
        Patient patient1 = patientService.getById(patientId);
        return patient1;
    }

    public Patient findByName(String patientName) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Patient::getPatientName, patientName);
        return patientService.getOne(queryWrapper);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        patientService = patient;
        patientDAO = this;
    }

    public List<Patient> getPatients() {
        return patients;
    }

    public Boolean updatePatient(Patient patient) {
        return patientService.updateById(patient);
    }

    public Patient updatePatientName(Patient patient) {
        return patientService.updatePatientNameById(patient);
    }

    public String getPatientInfo(Patient patient) {
        StringBuilder str = new StringBuilder();
        str.append("病人登记信息：" + "\n病历号：").append(patient.getPatientId()).append("\n病人姓名：")
                .append(patient.getPatientName()).append("\n性别：").append(patient.getSex()).append("\n挂诊科室：").
                append(patient.getDeptName()).append("\n病房号：").append(patient.getWardNo()).append("\n病床号: ").
                append(patient.getBedNo()).append("\n第几床：").append(patient.getTheBedNo()).append("\n主治医生：").
                append(patient.getDoctorName()).append("\n病情:\n").append(patient.getIllness());
        return str.toString();
    }

}
